package com.wswy.wyjk.ui.practiceNew;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.R;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.ui.practiceNew.Adapter.ExamRecordAdapter;
import com.wswy.wyjk.ui.practiceNew.Model.ExaminationRecord;

@ViewL(R.layout.fragment_exam_record)
public class ExamRecordActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private ExamRecordAdapter examRecordAdapter;
    private TextView tvRecordTop,tvTopToExam;
    private View rlTop;

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected String showTitle() {
        return "考试记录";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {

    }

    private void initView() {
        isShowTitleLeft(View.VISIBLE);
        rlTop = findViewById(R.id.rl_top);
        tvTopToExam=findViewById(R.id.tv_top_to_exam);
        tvRecordTop = findViewById(R.id.tv_record_top);
        recyclerView = findViewById(R.id.recyclerView);
        View emptyView = LayoutInflater.from(this).inflate(R.layout.layout_exam_record_empty, null);
        TextView tv_empty_to_exam=emptyView.findViewById(R.id.tv_empty_to_exam);
        tv_empty_to_exam.setOnClickListener(view -> tvTopToExam.performClick());
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        examRecordAdapter = new ExamRecordAdapter();
        examRecordAdapter.setEmptyView(emptyView);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        recyclerView.setAdapter(examRecordAdapter);
    }

    private void initData() {
        String record = MMKV.defaultMMKV().decodeString(MMKVConstant.RECORD);
        ExaminationRecord examinationRecord = new Gson().fromJson(record, ExaminationRecord.class);
        if (examinationRecord != null) {
            examRecordAdapter.setNewData(examinationRecord.getList());
            tvRecordTop.setText(getString(R.string.exam_record_top, examinationRecord.getMaxFraction()));
        } else {
            rlTop.setVisibility(View.GONE);
        }
    }

    private void initListener(){
        tvTopToExam.setOnClickListener(view -> {
            intentFinish(ExamActivity.class);
        });
    }
}
